package com.gxar.quick.ar.server.base.config;

import com.gxar.common.utils.IpUtils;
import com.gxar.quick.ar.server.base.common.properties.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * xxl 配置
 *
 * @author linsy
 * @data 2023/11/11
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class XxlJobConfig {

    private final XxlJobProperties xxlJobProperties;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminProperties().getAddresses());
        xxlJobSpringExecutor.setAppname(xxlJobProperties.getExecutorProperties().getAppName());
        String ip = xxlJobProperties.getExecutorProperties().getIp();
        if (StringUtils.isBlank(ip)) {
            String ipStart = IpUtils.getIpStart(xxlJobProperties.getExecutorProperties().getStartWithIp());
            if (StringUtils.isNotBlank(ipStart)) {
                ip = ipStart;
            }
        }
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(xxlJobProperties.getExecutorProperties().getPort());
        xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlJobProperties.getExecutorProperties().getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getExecutorProperties().getLogRetentionDays());
        return xxlJobSpringExecutor;
    }

}
